package MainTest;

import javax.sound.midi.*;

public class MiniMiniMusicApp {
    public static void main(String[] args) {
        MiniMiniMusicApp mini = new MiniMiniMusicApp();
        mini.play();
    }
    public void play() {
        try{
            //取得Sequence并将其打开
            Sequencer player = MidiSystem.getSequencer();
            player.open();
            //
            Sequence seq = new Sequence(Sequence.PPQ,4);
            //要求取得track
            Track track = seq.createTrack();
            //对track加入几个midievent,要注意的是setmessage的参数,以及midievent的constructor
            ShortMessage a = new ShortMessage();
            a.setMessage(144,2,102,100);
            MidiEvent noteOn = new MidiEvent(a,1);
            track.add(noteOn);

            ShortMessage b = new ShortMessage();
            a.setMessage(128,2,102,100);
            MidiEvent noteOff = new MidiEvent(b,6);
            track.add(noteOff);

            player.setSequence(seq);
            player.start();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
